package com.zijie.hosptal.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zijie.hosptal.exception.DsException;
import com.zijie.hosptal.mapper.DepartmentMapper;
import com.zijie.hosptal.mapper.DoctorSchedulesMapper;
import com.zijie.hosptal.pojo.po.Department;
import com.zijie.hosptal.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {


    private DepartmentMapper departmentMapper;
    private DoctorSchedulesMapper doctorSchedulesMapper;

    @Autowired
    public void setDoctorSchedulesMapper(DoctorSchedulesMapper doctorSchedulesMapper) {
        this.doctorSchedulesMapper = doctorSchedulesMapper;
    }

    @Autowired
    public void setDepartmentMapper(DepartmentMapper departmentMapper) {
        this.departmentMapper = departmentMapper;
    }


    @Override
    public Page<Department> list(Page<Department> departmentPage, Department department) {
        Page<Department> p=departmentMapper.findList(departmentPage,department);
        return p;
    }

    @CacheEvict(value = "department",key = "'getAllDepartmentList'")
    @Override
    public boolean save(Department department) {
        int insert = departmentMapper.insert(department);
        return insert > 0;
    }

    @CacheEvict(value = "department",key = "'getAllDepartmentList'")
    @Override
    public boolean removeById(Integer id) {
        Integer i = doctorSchedulesMapper.selectNamalByDepartmentId(id);
        //如果i不等于0说明这个科室还要有活动不能删除
        if (i != null) {
            throw new DsException("科室要举行义诊活动，不能删除");
        }
        int delete = departmentMapper.deleteById(id);
        return delete > 0;
    }

    @CacheEvict(value = "department",key = "'getAllDepartmentList'")
    @Override
    public boolean updateById(Department department) {
        int update = departmentMapper.updateById(department);
        return update > 0;
    }

    @CacheEvict(value = "department",key = "'getAllDepartmentList'")
    @Override
    public boolean updateDepartmentPrice(Department department) {
        int update = departmentMapper.updateDepartmentPrice(department);
        return update > 0;
    }

    @Cacheable(value = "department",key = "'getAllDepartmentList'")
    @Override
    public List<Department> getAll() {
        return departmentMapper.selectList( null);
    }
}
